<template>
  <!--size抽屉宽度（或高度）  default 378px 和 large 736px -->
  <a-drawer
      v-model:open="visible"
      ref="employeeDetailModalRef"
      class="edit-drawer modal-fixed-bottom"
      root-class-name="root-customer-drawer"
      width="800px"
      :closable="false"
      @close="onClose"
  >
    <div class="closeable" @click="onClose">{{ $t('self.closeButton') }}</div>
    <div class="modal-header">{{ title }}</div>
    <a-form :model="formState" :rules="formRules" :label-col="labelCol" :wrapper-col="wrapperCol"
            @finish="onSubmit"
    >
      <a-form-item :label="$t('self.systemParams.tips4')" name="remark">
        <a-input allow-clear v-model:value="formState.remark" placeholder="" />
      </a-form-item>
      <a-form-item :label="$t('self.systemParams.tips5')" name="paraValue">
        <a-input allow-clear v-model:value="formState.paraValue" placeholder="" />
      </a-form-item>
      <a-form-item :label="$t('self.systemParams.tips6')" name="type">
        <a-input allow-clear v-model:value="formState.type" placeholder="" />
      </a-form-item>
<!--      <a-form-item :label="$t('self.systemParams.tips6')" name="type">-->
<!--        <a-select v-model:value="formState.type" :options="[-->
<!--            { label: $t('self.systemParams.types.tips1'), value: 1 },-->
<!--            { label: $t('self.systemParams.types.tips2'), value: 2 },-->
<!--        ]" :placeholder="$t('self.pleaseChoose')" />-->
<!--      </a-form-item>-->
      <a-form-item :label="$t('self.systemParams.tips8')">
        <a-switch v-model:checked="formState.isUse" :checked-children="$t('self.yesTxt')" :un-checked-children="$t('self.noTxt')" :checkedValue="1" :unCheckedValue="0" />
      </a-form-item>


      <div class="base-flex base-rows base-align-items-center base-justify-content-center footer">
        <a-button type="primary" html-type="submit">{{ $t('self.submitButton') }}</a-button>
        <a-button class="base-ml10" @click="onClose">{{ $t('self.cancelButton') }}</a-button>
      </div>

    </a-form>

  </a-drawer>
</template>

<script setup>
import { computed, getCurrentInstance, onMounted, ref } from 'vue'
import { message } from 'ant-design-vue'
const { proxy } = getCurrentInstance()
const $TOOL = proxy.$TOOL
const $API = proxy.$API
const $T = proxy.$t
const emit = defineEmits(['close','ok'])

const visible = ref(false)
// 模式，add新增，show查看，edit编辑
const mode = ref('add')
const labelCol = {
  style: {
    width: '230px',
  },
};
const wrapperCol = {
  span: 16,
};
const title = ref('')
const formState = ref({
  id: '',
  remark: '',
  paraValue: '',
  type: null,
  isUse: 1,
})
const formRules = ref({
  orgName: [{required: true,message: $T('self.pleaseInput') + $T('self.operators.tips1'),trigger: 'change'}],
  phone: [{required: true,message: $T('self.pleaseInput') + $T('self.operators.tips2'),trigger: 'change'}],
})

onMounted(() => {

})

const onSubmit = (values) => {
  const params = { ...formState.value }
  if(mode.value === 'add') {
    // 添加
    $API.backend.systemParams.add.post(params).then(res => {
      message.success($T('self.message.success'))
      onReset()
      visible.value = false
      emit('ok')
    }).catch(err => {
      message.error(err.msg);
    })
  }else{
    // 修改
    $API.backend.systemParams.edit.post(params).then(res => {
      message.success($T('self.message.success'))
      onReset()
      visible.value = false
      emit('ok')
    }).catch(err => {
      message.error(err.msg);
    })
  }
  // console.log(formState.value)
  // console.log('submit!', values);
}

const onReset = () => {
  // 清空内容
  formState.value = {
    id: '',
    remark: '',
    paraValue: '',
    type: null,
    isUse: 1,
  }
}

const onClose = () => {
  onReset()
  visible.value = false
  emit('close')
}

const add = () => {
  mode.value = 'add'
  title.value = $T('self.systemParams.tips1')
  visible.value = true
}

const edit = (record) => {
  mode.value = 'edit'
  // formState.value = record
  title.value = $T('self.systemParams.tips2')
  visible.value = true
  console.log(record)
  formState.value = {
    id: record.id,
    remark: record.paraDesc,
    paraValue: record.paraValue,
    type: record.paraType,
    isUse: record.enbaled,
  }
  // detail.value = record
  // $TOOL.array.cloneExistKey(formState.value, detail.value)
  // console.log(formState.value)
}

// 子组件中要通过defineExpose将方法暴露给父组件
defineExpose({
  add,
  edit,
})
</script>

<style lang="scss" scoped>
.edit-drawer {
  position: relative;
  .closeable {
    @include closeable;
  }
}
</style>
